package librus.codebase.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import librus.codebase.image.Processor;

public class Window extends JFrame implements ActionListener {

	private static final long serialVersionUID = -6140167744289058414L;

	
	private JCheckBox resize;
	private JCheckBox dither;
	
	private JButton runButton;
	private JLabel progress;
	
	
	public Window()
	{
		super("Resizer");
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setSize(300, 100);
		setResizable(false);		
		createUI();
				
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (int) ((dimension.getWidth() - getWidth()) / 4);
		int y = (int) ((dimension.getHeight() - getHeight()) / 4);
		setLocation(x, y);
	
		setVisible(true);
	}
	
	private void createUI()
	{
		
		resize = new JCheckBox("Resize");
		resize.setSelected(true);
		getContentPane().add(resize);
		
		dither = new JCheckBox("Dither");
		dither.setSelected(true);
		getContentPane().add(dither);
		
		
		JPanel checkPanel = new JPanel(new GridLayout(0, 1));
        checkPanel.add(resize);
        checkPanel.add(dither);
        
        add(checkPanel, BorderLayout.LINE_START);
                
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));        
                
        runButton = new JButton("Select path & run");
        runButton.addActionListener(this);        
        panel.add(runButton);
        
        add(panel,BorderLayout.SOUTH);
        
        progress = new JLabel("");
        add(progress, BorderLayout.LINE_END);
	}

	public void actionPerformed(ActionEvent e) 
	{		
		JFileChooser chooser = new JFileChooser(new File("C:/MyData/"));
	    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	   
	    int returnVal = chooser.showOpenDialog(this);
	    if(returnVal == JFileChooser.APPROVE_OPTION) {
	    	
	    	Processor processor = new Processor(chooser.getSelectedFile().getAbsolutePath(),
	    			resize.isSelected(), dither.isSelected(), progress, runButton);
	    	processor.start();
	    }
		
	}
}
